<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Foo extends MY_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
     function __construct(){
        parent::__construct();
        parse_str($_SERVER['QUERY_STRING'],$_GET);
        $error=array();
     }
	public function index()
	{
		$this->foo2();
	}
    public function foo2(){
        
        $data=NULL;
        
        //debug($this);
		
		
		//$data['html'] =  Modules::run('bar/bar/index');
        
		$this->template->set('a','A');
		$this->view('foo_msg',$data);
     
    }
	
	public function overlay(){
		$html = $this->baseView('foo_search','',true);
		echo json_encode(array('html'=>$html));
	}
    
    function foo_search(){
        $html=$this->baseView('foo_search','',true);
        echo json_encode(array('html'=>$html));
    }
    /** Login With OpenID ***/
    function login(){
        $this->loginView('loginplus');
    }
    
    public function loginplus(){
        if($_GET['provider']  && ($_GET['provider'] == 'google' || $_GET['provider'] == 'yahoo' )){
        $provider = $_GET['provider']; 
        $this->load->library('openid');
        $this->load->model('member_model');
        	try {
                # Change 'localhost' to your domain name.
                $openid = new Openid('localhost');
               // $openid->returnUrl=BASE_URL."index.php/foo/display";
                $openid->required = array('namePerson/first','namePerson/last','contact/email' ,'birthDate/birthYear','namePerson/prefix','namePerson/friendly');
                if(!$openid->mode) {
                        if($provider == 'google')
                        {
                            $openid->identity = 'https://www.google.com/accounts/o8/id';    
                        }else if($provider == 'yahoo'){
                            $openid->identity = 'https://me.yahoo.com';    
                        }else{
                            return false;
                        }
                        redirect($openid->authUrl(), ‘refresh’);
                } elseif($openid->mode == 'cancel') {
                    $data['error']=__("Người dùng đã hủy bỏ đăng nhập.");
                    $this->loginView('error',$data);
                    return;
                    
                } else {
                    if($openid->validate()){
                        #check valid user on Provider
                        #Get attributes respond
                        #check User Exists on DB 
                        #1.Exists Start User Session 
                        #2.Not Exists Add to DB and Star User Session
                        $userdata=$openid->getAttributes();
                        $checkMember=$this->member_model->getMemberByEmail($userdata['contact/email']);
                        
                        if(empty($checkMember)){
                            // Email not exist -> add to db
                            $lastname = '';
                            if($provider == 'google'){
                                $lastname = $userdata['namePerson/last'];
                            }else 
                            if($provider == 'yahoo'){
                                $lastname = $userdata['namePerson/friendly'];
                            }
                            
                            $objMember = array(
                            'firstName' => $userdata['namePerson/first'],
                            'lastName' => $lastname,
                            'username' => $userdata['contact/email'],
                            'email' => $userdata['contact/email'],
                            'departmentId' => 1
                            );
                            
                            $result = $this->member_model->insertMember($objMember);
                            $objMemberNew=$this->member_model->getMemberByEmail($userdata['contact/email']);
                            if($result==1){
                            $this->session->set_userdata('LoginData',$objMemberNew); 
                            redirect(BASE_URL);    
                            }
                        }else{
                            $this->session->set_userdata('LoginData',$checkMember); 
                            redirect(BASE_URL); 
                        }                         
                        }else {
                            
                            $data['error']=__("Tài khoản không hợp lệ.");
                            $this->loginView('error',$data);
                            return;
                        }
                }
            } catch(ErrorException $e) {
               $data['error']=$e->getMessage();
               $this->loginView('error',$data);
               return;
            }
            }else{
                // provider not support return login page
                redirect('/foo/login');
            }
            
            
    }
    
    
    
    
    
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */